<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>magedu</title>
    </head>
    <body>
        <h1>马哥教育www.magedu.com -- http库、WebSocket使用</h1>
        <br />
        WebSocket测试 ws://127.0.0.1:9999/wsecho
    </body>
</html>
<script>
    // 利用http协议先握手，请求头中有Sec-Websocket-Key:[1g4Tvo2jzXGnsGDgN0JdEA==] Sec-Websocket-Version:[13] Upgrade:[websocket]
    const ws = new WebSocket("ws://127.0.0.1:9999/wsecho");
    // Connection opened
    ws.addEventListener("open", function (event) {
    ws.send("Hello Server!");
    });
    // Listen for messages
    ws.addEventListener("message", function (event) {
    console.log("Message from server ", event.data);
    });
    //
    ws.addEventListener("error", (event) => {
    console.log("连接错误：", event);
    });
    // 连接关闭回调
    ws.onclose = (event) => {
    console.log("连接关闭");
    };
</script>